package com.solo.eggsolo.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.solo.eggsolo.common.bean.JsonResult;
import com.solo.eggsolo.common.bean.Pager;
import com.solo.eggsolo.entity.User;
import com.solo.eggsolo.service.UserService;
import com.solo.eggsolo.util.FileResult;
import com.solo.eggsolo.util.FileuploadUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @RestController控制器默认会返回Body
 * @author yu
 *
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
@Slf4j
public class UserController {

	@Autowired
	private UserService userService;

	@Value("${spring.servlet.multipart.location}")
	private String uploadPath;

	@ApiOperation("获取所有用户的信息")
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public Pager<User> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer size) {

		Pager<User> pager = new Pager<>(page, size);
		List<User> list = userService.list(pager);
		int total = userService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}

	@ApiOperation("根据用户id查询用户信息")
	@RequestMapping(value = "/findById", method = { RequestMethod.GET })
	public JsonResult findById(Integer id) {

		JsonResult result = new JsonResult(500, Boolean.FALSE, "查询失败");
		User user = userService.findById(id);

		if (user != null) {
			result = new JsonResult(200, Boolean.TRUE, "查询成功", user);
		}
		return result;
	}

	@ApiOperation("根据用户名字模糊查询用户信息")
	@RequestMapping(value = "/findByName", method = { RequestMethod.GET })
	public Pager<User> findByName(User user, String name) {

		Pager<User> pager = new Pager<>(1, 100);
		List<User> list = userService.findByName(name);
		int total = userService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}
	@ApiOperation("通过token获取用户信息")
	@RequestMapping(value = "/userInfo", method = { RequestMethod.GET })
	public JsonResult userInfo(String token) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "查询失败");
		User user = userService.userInfo(token);

		if (user.equals(user)) {
			result = new JsonResult(200, Boolean.TRUE, "查询成功", user);
		}
		return result;

	}
	@ApiOperation("增加用户（password和name是必填项）")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public JsonResult add(MultipartFile file1, User user) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "新增失败");

		// 先执行文件上传
		if (file1 != null && !file1.isEmpty()) {

			FileResult fileResult = FileuploadUtil.uploadFile(uploadPath, file1);
			if (!fileResult.getSuccess()) {
				return new JsonResult(false, fileResult.getMsg());
			}

			String serverFileString = fileResult.getServerPath();
			// 把上传后的文件地址保存到用户的参数中
			user.setAvatar(serverFileString);
		}

		// 用户注册时，取服务器的系统时间
		user.setCreateTime(new Date());

		boolean ret = userService.add(user);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "新增成功");
		}
		return result;
	}
	@ApiOperation("根据用户id编辑用户信息")
	@RequestMapping(value = "/edit", method = { RequestMethod.POST })
	public JsonResult edit(MultipartFile file1, User user) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "修改失败");

		// 先执行文件上传
		if (file1 != null && !file1.isEmpty()) {

			FileResult fileResult = FileuploadUtil.uploadFile(uploadPath, file1);
			if (!fileResult.getSuccess()) {
				return new JsonResult(false, fileResult.getMsg());
			}
			String serverFileString = fileResult.getServerPath();
			// 把上传后的文件地址保存到用户的参数中
			user.setAvatar(serverFileString);
		}

		// 先上传后更新（保存）
		boolean ret = userService.edit(user);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "修改成功");
		}
		return result;
	}
	@ApiOperation("根据用户id重置密码")
	@RequestMapping(value = "/resetPassword", method = { RequestMethod.POST })
	public JsonResult resetPassword(Integer id) {

		JsonResult result = new JsonResult(500, Boolean.FALSE, "重置失败");

		boolean ret = userService.resetPassword(id);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "重置成功");
		}
		return result;
	}
	@ApiOperation("根据用户id删除用户")
	@RequestMapping(value = "delete", method = { RequestMethod.POST })
	public JsonResult delete(User user) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "删除失败");

		List<User> list = new ArrayList<User>();
		list.add(user);

		boolean ret = userService.deleteByIds(list);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "删除成功");
		}
		return result;
	}

}